#pragma GCC optimize(2, "Ofast", "inline")
#include <iostream>
#include <vector>
#include <string>
#define endl '\n'
#define int long long
using namespace std;

struct Node
{
    Node* next=nullptr;
    string str="";
    Node(const string& s, Node* n = nullptr) : next(n), str(s) {}
};

int n, q, t, p;
string s;
Node *pc[222222];
Node* server;
string ans="";

signed main()
{
    cin.tie(0)->ios::sync_with_stdio(false);
    cin >> n >> q;    
    while (q--)
    {
        cin >> t >> p;
        if (t == 1) pc[p] = server;
        else if (t == 2)
        {
            cin >> s;
            pc[p] = new Node(s, pc[p]); // 头插法
        }
        else server = pc[p];
    }

    // 收集结果
    vector<string> result;
    for (Node* cur = server; cur != nullptr; cur = cur->next)
    {
        if (!cur->str.empty())
        {  // 跳过空字符串
            result.push_back(cur->str);
        }
    }
    // 反转顺序并输出
    for (int i = result.size() - 1; i >= 0; i--)
    {
        cout << result[i];
    }
    return 0;
}